package com.miniapp.core.service.impl;

import com.alibaba.fastjson2.JSONObject;

import com.miniapp.common.core.domain.AjaxResult;
import com.miniapp.common.core.domain.entity.miniapp.MiniAppUser;
import com.miniapp.common.core.domain.model.LoginUser;
import com.miniapp.common.gen.domain.PaicFtiAgent;
import com.miniapp.common.gen.domain.PaicFtiAgentExample;
import com.miniapp.common.gen.domain.PaicFtiRate;
import com.miniapp.common.gen.domain.PaicFtiRateExample;
import com.miniapp.common.gen.mapper.PaicFtiAgentMapper;
import com.miniapp.common.gen.mapper.PaicFtiRateMapper;
import com.miniapp.common.utils.MoneyUtil;
import com.miniapp.common.utils.SecurityUtils;
import com.miniapp.common.utils.StringUtils;
import com.miniapp.core.domain.PaicPolicyDetailsResult;
import com.miniapp.core.domain.PaicPolicyParam;
import com.miniapp.core.domain.PaicPolicyResult;
import com.miniapp.core.domain.paicfti.PaicFtiInsureParam;
import com.miniapp.core.domain.paicfti.PremiumCalculationParam;
import com.miniapp.core.mapper.PaicFtiMapper;
import com.miniapp.core.service.IPaicFtiService;
import com.miniapp.core.utils.PaicFtiParamUtils;
import com.miniapp.core.utils.PaicFtiUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PaicFtiServiceImpl implements IPaicFtiService {

    private Logger logger = LoggerFactory.getLogger(PaicFtiServiceImpl.class);

    @Autowired
    private PaicFtiUtils paicFtiUtils;

    @Autowired
    private PaicFtiParamUtils paicFtiParamUtils;

    @Autowired
    private PaicFtiRateMapper paicFtiRateMapper;

    @Autowired
    private PaicFtiAgentMapper paicFtiAgentMapper;

    @Autowired
    private PaicFtiMapper paicFtiMapper;


    /**
     * 平安货运险投保
     * @param paicFtiInsureParam
     * @return
     */
    @Override
    public AjaxResult insure(PaicFtiInsureParam paicFtiInsureParam) {
        logger.info("平安货运险投保参数为：{}",JSONObject.toJSONString(paicFtiInsureParam));
        //0、投保参数验证
        AjaxResult ajaxResult = paicFtiParamUtils.paramVerification(paicFtiInsureParam);
        if(ajaxResult.isError()){
            logger.info("投保参数验证失败返回：",JSONObject.toJSONString(ajaxResult));
            return ajaxResult;
        }else {
            //投保
            logger.info("参数验证通过，开始投保");
            paicFtiUtils.asynInsure(paicFtiInsureParam);
            return AjaxResult.success("提交成功");
        }
    }


    /**
     * 获取保单列表
     * @param paicPolicyParam
     * @return
     */
    @Override
    public List<PaicPolicyResult> policyList(PaicPolicyParam paicPolicyParam) {
        Boolean isMiniApp = SecurityUtils.getLoginUser().getIsMiniApp();
        if(isMiniApp){
            LoginUser loginUser = SecurityUtils.getLoginUser();
            MiniAppUser miniAppUser = loginUser.getMiniAppUser();
            String purePhoneNumber = miniAppUser.getPurePhoneNumber();
//        String purePhoneNumber = "13381227115";
            if(StringUtils.isBlank(purePhoneNumber)){
                return new ArrayList<PaicPolicyResult>();
            }
            paicPolicyParam.setAgentPhone(purePhoneNumber);
        }

        List<PaicPolicyResult> paicPolicyResults = paicFtiMapper.policyList(paicPolicyParam);
        return paicPolicyResults;
    }

    /**
     * 查询平安货运险保单详情
     * @param policyId
     * @return
     */
    @Override
    public PaicPolicyDetailsResult policyDetails(Integer policyId) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        MiniAppUser miniAppUser = loginUser.getMiniAppUser();
        String phoneNumber = miniAppUser.getPurePhoneNumber();
        PaicPolicyDetailsResult paicPolicyDetailsResult = paicFtiMapper.policyDetails(policyId, phoneNumber);
        return paicPolicyDetailsResult;
    }

    /**
     * 计算保费
     * @param premiumCalculationParam
     * @return
     */
    @Override
    public AjaxResult premiumCalculation(PremiumCalculationParam premiumCalculationParam) {
        //查询产品
        String productType = premiumCalculationParam.getProductType();
        //保额/发票金额 分
        Long insuranceAmount = premiumCalculationParam.getInsuranceAmount();
        if(StringUtils.isBlank(productType) || insuranceAmount ==null ){
            return AjaxResult.error("产品方案、保额不能为空");
        }else if(insuranceAmount == 0){
            return AjaxResult.error("发票金额不能0");
        }
        PaicFtiRateExample paicFtiRateExample = new PaicFtiRateExample();
        paicFtiRateExample.createCriteria().andProductTypeEqualTo(productType);
        List<PaicFtiRate> paicFtiRateList = paicFtiRateMapper.selectByExample(paicFtiRateExample);
        if(CollectionUtils.isEmpty(paicFtiRateList)){
            return AjaxResult.error("未查询到方案费率");
        }
        PaicFtiRate paicFtiRate = paicFtiRateList.get(0);

        //费率 %
        String rate = paicFtiRate.getRate();

        //保费 元
        String priceYuan = MoneyUtil.moneyMul(rate, insuranceAmount.toString());
        priceYuan = MoneyUtil.moneydiv(priceYuan,"10000");
        //保费
        Double doublePriceYuan = Double.valueOf(priceYuan);
        //最低保费10元
        if(doublePriceYuan < 10.00){
            priceYuan = "10.00";
        }

        //保额 元
        String amountYuan = MoneyUtil.moneydiv(insuranceAmount.toString(), "100");

        //判断 费率是否大于1 保费 / 保额
        String realityRate = MoneyUtil.moneydivFiveRoundItUp(priceYuan, amountYuan);
        Double doubleRate = Double.valueOf(realityRate);
        if(doubleRate > 1){
            return AjaxResult.error("费率控制在0.00000001至1之间");
        }

        Double aDouble = Double.valueOf(MoneyUtil.moneyMul(priceYuan, "100"));
        return AjaxResult.success("成功",aDouble.intValue());
    }







    public static void main(String[] args) {
        String s = MoneyUtil.moneydivFiveRoundItUp("18", "100000");
        System.out.println(s);


    }


}
